package com.lingshi.shopping_order_service.service;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.lingshi.shopping.constant.Constant;
import com.lingshi.shopping.entity.CartGoods;
import com.lingshi.shopping.entity.Orders;
import com.lingshi.shopping.enums.OrderStatusEnums;
import com.lingshi.shopping.service.CartService;
import com.lingshi.shopping.service.OrdersService;
import com.lingshi.shopping_order_service.mapper.CartGoodsMapper;
import com.lingshi.shopping_order_service.mapper.OrdersMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Slf4j
@DubboService
@Transactional
public class OrdersServiceImpl implements OrdersService {

    @Autowired
    private OrdersMapper ordersMapper;

    @Autowired
    private CartGoodsMapper cartGoodsMapper;

    @DubboReference
    private CartService cartService;

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Override

    public Orders add(Orders orders) {

        /**
         * 此处两种情况新增订单到数据库
         * 1， 正常下单，提交没有状态，没有过期时间，没有创建时间，需要单独设置
         * 2， 秒杀订单redis数据同步mysql : 只需要同步到mysql即可，不用设置其他参数
         */

        //普通购买订单那
        if(StrUtil.isBlank(orders.getStatus())){
            String orderId = IdUtil.getSnowflakeNextIdStr();
            orders.setId(orderId);
            orders.setCreateTime(new Date());
            //设置过期时间24小时
            DateTime expireTime = DateUtil.offsetSecond(orders.getCreateTime(), 25);
            orders.setExpire(expireTime);
            orders.setStatus(OrderStatusEnums.UNPAID.getCode());
        }
        //获取订单id
        String orderId = orders.getId();


        //新增订单
        ordersMapper.insert(orders);

        List<CartGoods> cartGoodsList = orders.getCartGoods();
        for (CartGoods cartGoods : cartGoodsList) {
            cartGoods.setOrderId(orderId);
            //新增订单明细
            cartGoodsMapper.insert(cartGoods);

            //删除购物车商品数据
            Long userId = orders.getUserId();
            cartService.deleteCartGoods(userId,cartGoods.getGoodId());
        }
        ////延时等级1到16分别表示 1s 5s 10s 30s 1m 2m 3m 4m 5m 6m 7m 8m 9m 10m 20m 30m 1h 2h
        log.info("新增订单订单id ： {}", orderId);
        Message<String> message = MessageBuilder.withPayload(orderId).build();
        //将定id发送消息队列
        rocketMQTemplate.syncSend(Constant.CHECK_ORDER_STATUS_TOPIC, message ,5000,4);

        return orders;
    }

    @Override
    public void update(Orders orders) {
        ordersMapper.updateById(orders);
    }

    @Override
    public Orders findById(String id) {
        return ordersMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Orders> findUserOrders(Long userId, Integer status) {
        return ordersMapper.getOrdersByUserIdOrderStatus(userId,status);
    }
}
